using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ProjectEuler.Core.Helpers;

namespace ProjectEuler.Core
{
    public class Problem32 : IProjectEulerProblem
    {
        public class Identity
        {
            public int Multiplicand { get; set; }
            public int Multiplier { get; set; }
            public int Product { get { return Multiplicand * Multiplier; } }
            public bool IsPandigital
            {
                get
                {
                    var nums = Multiplicand + Multiplier.ToString() + Product;
                    return MathHelper.IsPandigital(nums, "123456789");
                }
            }
        }

        protected IEnumerable<Identity> BuildPandigitalIdentities()
        {
            var list = new List<Identity>();
            for (int i = 1; i <= 2000; i++)
            {
                for (int j = 1; j < 300; j++)
                {
                    var ident = new Identity();
                    ident.Multiplier = i;
                    ident.Multiplicand = j;
                    if (ident.IsPandigital && !list.Any(item => item.Product == ident.Product))
                    {
                        list.Add(ident);
                    }
                }
            }
            return list;
        }

        public int Number
        {
            get { return 32; }
        }

        public string Description
        {
            get { return "Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital."; }
        }

        public string Answer
        {
            get
            {
                return BuildPandigitalIdentities().Sum(ident => ident.Product).ToString();
            }
        }
    }

    [TestFixture]
    public class Problem32Tests : Problem32
    {
        [Test]
        public void Test_that_7254_products_identity_is_pandigital()
        {
            var identity = new Identity();
            identity.Multiplicand = 39;
            identity.Multiplier = 186;
            Assert.IsTrue(identity.IsPandigital);
        }
    }
}